<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/3
 * Time: 15:29
 */

declare(strict_types=1);

namespace app\admin\controller;

use think\Request;
use  app\admin\traits\ValidateTrait;
use think\exception\ValidateException;

class Classification extends Base
{
    use ValidateTrait; // 使用 Trait
    ##员工信息
    protected $request;
    protected $service;
    protected $operator_id;
    protected $validationScenes = [
        'index' => [
            'pageIndex' => ['require|integer', '页码'],
            'pageSize' => [['require', 'between:10,100'], '每页数量'],
        ],
        'delete' => [
            'id' => ['require|integer', 'id'],
        ],
        'create' => [
            'cc_name' => ['require', '分类名称'],
        ],
        'show' => [
            'id' => ['require|integer', 'id'],
        ],
        'update' => [
            'id' => ['require|integer', 'id'],
        ],
        'distributCountry' => [
            'id' => ['require|integer', 'id'],
            'country_ids' => ['require|string', '国家id'],
        ],
        'availableWarehouse' => [
            'id' => ['require|integer', 'id'],
            'ids' => ['require|string', '可售仓库id'],
        ],
        'select' => [
            'id' => ['require', 'id'],
        ],
    ];

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\admin\service\Classification();

        $this->operator_id = $request->operator_id;
    }

    public function getSelect(Request $request)
    {
        $where = ['is_del' => 0];
        $res = $this->service->getSelect($where);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 分类列表
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function index()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('index');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage() ?? '';
            return $this->returnJsonData(100001, [], $customErrors);
        }
        $pageIndex = isset($params['pageIndex']) ? intval($params['pageIndex']) : 1;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 10;
        $where = ['is_del' => 0];
        $res = $this->service->getList($where, $pageIndex, $pageSize);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }


    /**
     * @describe 分类列表
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function sellableList()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('select');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage() ?? '';
            return $this->returnJsonData(100001, [], $customErrors);
        }
        $where = ['is_del' => 0];
        $res = $this->service->sellableList($where,$params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 获取销售人员详情
     * @date: 2024-08-16 03:07:37
     * @param [int] $id 物流公司ID @required
     * @return Renderable
     */
    public function show()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('show');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }
        $res  = $this->service->getOneById($params['id']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 创建分类
     * @date: 2024-08-16 03:09:17
     * @param [array] $data 创建物流公司的数据 @required
     * @return Renderable
     */
    public function create()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('create',$params);
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }
        $params['creator'] = $this->operator_id;
        $params['create_time'] = date('Y-m-d H:i:s');
        $result = $this->service->create($params);
        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * 更新销售人员
     * @date: 2024-08-16 03:11:17
     * @param [int] $id 物流公司ID @required
     * @param [array] $data 更新物流公司的数据 @required
     * @return Renderable
     */
    public function update()
    {

        $params = $this->request->params;

        try {
            $this->autoValidate('update',$params);
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }
        $params['updater'] = $this->operator_id;
        $params['update_time'] = date('Y-m-d H:i:s');
        $result = $this->service->update($params);

        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 删除分类
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function delete()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('delete');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage() ?? '';
            return $this->returnJsonData(100001, [], $customErrors);
        }
        $res = $this->service->delete($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 分配国家
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function distributCountry()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('distributCountry');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage() ?? '';
            return $this->returnJsonData(100001, [], $customErrors);
        }
        $res = $this->service->distributCountry($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 选择可售仓库
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function availableWarehouse()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('availableWarehouse');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage() ?? '';
            return $this->returnJsonData(100001, [], $customErrors);
        }
        $res = $this->service->availableWarehouse($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }


}